<template>
  <div class="app-container" style="text-align:center;">
  <!-- 导入对话框 -->
    <div style="padding-bottom:3% ">
        <el-upload
          ref="upload"
          :limit="1"
          accept=".dat"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :before-upload="beforeUpload"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip text-center" slot="tip" style="margin-bottom: 11px;">
            <span>仅允许导入DAT格式文件。</span>
          </div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确定上传</el-button>
<!--          <el-button @click="upload.open = false">取 消</el-button>-->
       </div>
    </div>

    <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="文件名" prop="file_name">
        <el-input
          v-model="queryParams.file_name"
          placeholder="请输入文件名"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="上传人" prop="user_name">
        <el-input
          v-model="queryParams.user_name"
          placeholder="请输入上传人"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['robot:aeration:remove']"
        >删除</el-button>
      </el-form-item>
    </el-form>
      <el-table v-loading="loading" :data="aerations" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="文件名" align="center" prop="file_name"/>
        <el-table-column label="上传人" align="center" prop="user_name"/>
        <el-table-column label="上传时间" align="center" prop="date_time">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.date_time) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:paramtype:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import {delAeration, listAeration} from "@/api/robot/aeration/aeration";
import {getToken} from "@/utils/auth";
import "@/assets/styles/robt-bg.css";
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      aerations: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      defaultProps: {
        label: "label"
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/robot/aeration/sendfiledat"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        user_name:undefined,
        file_name:undefined
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //上传附件
    beforeUpload(file){
      this.fileName = null;
      this.files = file;
      const extension = file.name.split('.')[1] === 'dat'
      const isLt2M = file.size / 1024 / 1024 < 20
      if (!extension) {
        this.fileName = null;
        this.files = null;
        this.$message.warning('上传只能是dat格式!')
        return false // 返回false不会自动上传
      }
      if (!isLt2M) {
        this.fileName = null;
        this.files = null;
        this.$message.warning('上传大小不能超过4MB!')
        return false // 返回false不会自动上传
      }
      this.fileName = file.name;
      return false // 返回false不会自动上传
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delAeration(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listAeration(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.aerations = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入配置DAT文件";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/user/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
